/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.titan.component.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.titan.component.IComponent;
import edu.ccut.titan.component.ILogicComponentInfo;
import edu.ccut.titan.component.INodeInfo;
import edu.ccut.titan.component.IParameterInfo;
import edu.ccut.titan.component.TitanComponentException;

/**
 * {@link ILogicComponentInfo} implementation
 * 
 * @see ILogicComponentInfos
 * @author LeslieGu
 * 
 */
public class TitanLogicComponentInfo extends AbstractComponentInfo implements
		ILogicComponentInfo {

	private Map<String, INodeInfo> nodeMap;

	private INodeInfo startNodeInfo;

	public TitanLogicComponentInfo(String symbolicName, String key,
			String name, String description) {

		super(symbolicName, key, name, description, null);

		this.nodeMap = new HashMap<String, INodeInfo>();
	}

	public INodeInfo getNodeInfo(String id) {
		return this.nodeMap.get(id);
	}

	public void addNodeInfos(List<INodeInfo> nodes)
			throws TitanComponentException {
		if (nodes != null) {
			for (INodeInfo nodeInfo : nodes) {
				this.addNodoInfo(nodeInfo);
			}
		}
	}

	public void addNodoInfo(INodeInfo nodeInfo) throws TitanComponentException {
		if (this.nodeMap.containsKey(nodeInfo.getId())) {
			throw new TitanComponentException("节点重复出现id = " + nodeInfo.getId());
		}

		this.nodeMap.put(nodeInfo.getId(), nodeInfo);
	}

	public INodeInfo getStartNodeInfo() {
		return this.startNodeInfo;
	}

	public void setStartNodeInfo(INodeInfo nodeInfo) {
		this.startNodeInfo = nodeInfo;
	}

	public IComponent createComponent() throws TitanComponentException {
		logger.debug("create logic component \"" + super.getFullKey() + "\"");
		return new TitanLogicComponent(this);
	}

	@Override
	public List<IParameterInfo> getParameterInfos() {
		return this.getStartNodeInfo().getParameterInfos();
	}
	
	
}
